{extend name="common/layout" /}
{block name="main"}
<div class="admin-main layui-anim layui-anim-upbit">
    <div class="padding30">
        <h3 class="page-box-title">{$title}</h3>
        <blockquote class="layui-elem-quote">
           {:lang('There are <i class="count"></i> tables in the database,Total <i class="size"></i>')}
            <a href="javascript:void(0)" style="float:right;" id="backUp" class="layui-btn borderrad5 layui-btn-sm pull-right layui-btn-normal">{:lang('BackUp')}</a>
            <div style="clear: both;"></div>
        </blockquote>
    </div>
    <div class="" id="_listBox">
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
</div>
{/block}
{block name="footer"}
<script type="text/html" id="size">
    {{d.size}}
</script>
<script type="text/html" id="action">
    <a class="" href="#" lay-event="optimize">{:lang('Optimization')}</a> | 
    <a class="" href="#" lay-event="repair">{:lang('Repair')}</a>
</script>
<script>
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        table.render({
            id: 'database',
            elem: '#list',
            url: '{:url("database")}',
            method: 'post',
            cols: [[
                {checkbox:true,fixed: true},
                {field: 'Name', title: "{:lang('Database Table')}", width: 150, fixed: true,sort: true},
                {field: 'Rows', title: "{:lang('Records')}", width: 150,sort: true},
                {field: 'Data_length', title: "{:lang('Occupy Space')}", width: 150,templet:'#size',sort: true},
                {field: 'Engine', title: "{:lang('Type')}", width: 110,sort: true},
                {field: 'Collation', title: "{:lang('Code')}", width: 150,sort: true},
                {field: 'Create_time', title: "{:lang('Creation Time')}", width: 180,sort: true},
                {field: 'Comment', title: "{:lang('Explain')}", width: 180},
                {width: 160, align: 'center', toolbar: '#action'}
            ]],
            done: function(res, curr, count){
                $('.count').html(res.tableNum);
                $('.size').html(res.total);
            }
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            loading = layer.load(1, {shade: [0.1, '#fff']});
            if (obj.event === 'optimize') {
                $.post("{:url('database/optimize')}",{tableName:data.Name},function(res){
                    layer.close(loading);
                    if(res.code > 0){
                        layer.msg(res.msg,{time:1000,icon:1},function(){
                            window.location.href = res.url;
                        });
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                    }
                });
            }else if(obj.event === 'repair'){
                $.post("{:url('database/repair')}",{tableName:data.Name},function(res){
                    layer.close(loading);
                    if(res.code > 0){
                        layer.msg(res.msg,{time:1000,icon:1},function(){
                            window.location.href = res.url;
                        });
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                    }
                });
            }
        });

        $('#backUp').click(function(){
            var obj = $(this);
            var checkStatus = table.checkStatus('database'); //test即为参数id设定的值
            var a = [];
            $(checkStatus.data).each(function(i,o){
                a.push(o.Name);
            });
            obj.addClass('layui-btn-disabled');
            obj.html("{:lang('Backup in...')}");
            $.post("{:url('database/backup')}",{tables:a},function(data){
                data = eval('('+data+')');
                if(data.code==1){
                    obj.removeClass('layui-btn-disabled');
                    obj.html("{:lang('BackUp')}");
                    layer.msg(data.msg,{time:1000,icon:1});
                }else{
                    layer.msg(data.msg,{time:1000,icon:2});
                }
            });
        })


    });
</script>
{/block}